package com.warrantyhotel.android.adapter;

import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.FindStoresTable;
import com.warrantyhotel.android.ui.WarrantyApplication;
import com.warrantyhotel.android.widget.CustomImageView;

public class NearbyStoresAdapter extends CursorAdapter {

	private LayoutInflater mInflater;
	private WarrantyApplication app;

	public NearbyStoresAdapter(Context context, Cursor c) {
		super(context, c, false);
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		app = (WarrantyApplication) context.getApplicationContext();

	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		final RssItemViewHolder listItem = (RssItemViewHolder) view.getTag();
		final String storesName = cursor.getString(cursor.getColumnIndex(FindStoresTable.NAME));
		final String thunbnail = cursor.getString(cursor.getColumnIndex(FindStoresTable.THUMBNAIL));

		listItem.txtNearByStoresName.setText(storesName);
		if (null != thunbnail && !TextUtils.isEmpty(thunbnail)) {
			listItem.storeImage.setTag(thunbnail);
			app.getImageLoaderNew().DisplayImage(thunbnail,  listItem.storeImage);
		} else {
			listItem.storeImage.setImageResource(R.drawable.newsphoto_placeholder);
		}

	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = mInflater.inflate(R.layout.nearby_stores_item, null);
		final RssItemViewHolder listItem = new RssItemViewHolder();
		listItem.txtNearByStoresName = (TextView) view.findViewById(R.id.nearby_stores_name_item);
		listItem.storeImage = (CustomImageView) view.findViewById(R.id.store_image);
		view.setTag(listItem);
		return view;
	}

	public class RssItemViewHolder {
		TextView txtNearByStoresName;
		CustomImageView storeImage;

	}

}
